using System.BusinessObjects.Transactions;
using Blazing.BusinessObjects.Cms;
using NHibernate;
using NHibernate.Criterion;

namespace Blazing.BusinessObjects.Queries
{
    public class QryFetchPageByMenuItem
    {
        public static IQuery Query(int menuItemId)
        {
            //ICriteria qry = UnitOfWork.CurrentSession.CreateCriteria(typeof(Contentpage));
            //qry.Add(Expression.Eq("MenuItems.ID", menuItemId));

            string hql = @"select p from Contentpage p join p.MenuItems m where m.ID=:menuItemId";
            IQuery qry = UnitOfWork.CurrentSession.CreateQuery(hql);
            qry.SetParameter("menuItemId", menuItemId);

            qry.SetCacheable(true);
            return qry;
        }

        public static IQuery Query(int rootPageId, int menuItemId)
        {
            //psudeo logic:
            //Get the page for a menu item who's pageid is rootPageId
            // or who is a child of rootPageId
            //or who is a 2nd child of rootPageId
            string hql = @"select p from Contentpage p 
                            join p.MenuItems m
                            where 
                            (p.ID = :pageId or
                            p.ParentPage.ID = :pageId or
                            p.ParentPage.ParentPage.ID = :pageId)
                            and
                            m.ID=:menuItemId";

            IQuery qry = UnitOfWork.CurrentSession.CreateQuery(hql);
            qry.SetParameter("pageId", rootPageId);
            qry.SetParameter("menuItemId", menuItemId);

            qry.SetCacheable(true);
            return qry;
        }
    }
}
